<?php

namespace App\V1_0_1;

use Phalcon\Loader,
    Phalcon\Mvc\View,
    \Phalcon\DI\Injectable,
    \Phalcon\DiInterface,
    App\Helpers\Utils;

/**
 * 模块类
 * @package App\V1_0_1
 */
class Module extends Injectable {

    /**
     * 模块相关配置
     * @var \Phalcon\Config
     */
    private $config;

    /**
     * 构造函数.
     */
    public function __construct() {
        $this->config = Utils::configItem("module");
    }

    /**
     * 自动加载特定模块的配置
     * @param \Phalcon\DiInterface $di
     */
    public function registerAutoloaders(DiInterface $di) {
        $loader = new Loader();
        $namespaces = array_merge($this->config->v1_0_1->namespaces->toArray(), $this->config->v1->namespaces->toArray());
        $loader->registerNamespaces($namespaces, true);
        $loader->register();
    }

    /**
     * 注册该模块特定的服务
     * @param \Phalcon\DiInterface $di
     */
    public function registerApps(DiInterface $di) {

        $v1_0_1 = $this->config->v1_0_1;
        /**
         * 设置视图组件
         */
        $di->setShared('view', function () use ($v1_0_1) {
            $view = new View();
            $view->setViewsDir(isset($v1_0_1->views) ? $v1_0_1->views : "");
            return $view;
        });
    }
}